/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package socketplayer;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class ServerSocketControl {

    class ROOM {

        public String id;
        public int time;
        public List<Integer> index;
        Timer timer;

        public ROOM() {
            index = new ArrayList<Integer>();
        }
    }
    ServerSocket server;
    int id = 0;
    String global_message;
    //Vector<socketAPI> clients;
    List<socketAPI> clients;
    List<ROOM> break_room;

    public ServerSocketControl() {
        try {
            server = new ServerSocket(899);
            clients = new ArrayList<socketAPI>();
            break_room = new ArrayList<ROOM>();
        } catch (IOException ex) {
            Logger.getLogger(SocketPlayer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void StartListen() {
        try {
            while (true) {
                Socket client = server.accept();
                socketAPI _clientThread = new socketAPI(client, id, this);
                clients.add(_clientThread);
                _clientThread.start();
                id++;
            }

        } catch (IOException e) {
            System.out.println("Accept failed: 4321");
            System.exit(-1);
        }
    }

    public String createRoom() {
        ROOM r = new ROOM();
        r.time = 30;
        r.id = UUID.randomUUID().toString();
        r.timer = new Timer();
        break_room.add(r);
        return r.id;
    }

    public void startSendTime(String roomID) {
        int room_location = getRoomByID(roomID);
        SendStartMessage stt = new SendStartMessage(this, roomID);
        break_room.get(room_location).timer.schedule(stt, 1 * 1000);
    }

    public boolean addToRoom(int _clientID, String roomid) {

        int room_location = getRoomByID(roomid);
        if (room_location == -1) {
            return false;
        }

        int current = 0;
        int idx;
        for (int i = 0; i < break_room.get(room_location).index.size(); i++) {
            idx = break_room.get(room_location).index.get(i);
            if (!clients.get(idx).isClose()) {
                current++;
            }
        }

        if (current == 5) {
            return false;
        }
        if(break_room.get(room_location).time ==0 )
            return false;
        break_room.get(room_location).index.add(_clientID);

        return true;
    }

    public int getRoomByID(String roomid) {
        int rid = -1;
        for (int i = 0; i < break_room.size(); i++) {
            if (break_room.get(i).id.equalsIgnoreCase(roomid)) {
                rid = i;
            }
        }
        return rid;
    }

    public boolean SendMessage(String roomid, int type) {
        int idx;
        int room_location = getRoomByID(roomid);
        if (room_location == -1) {
            return false;
        }
        if (type == 0) {
            global_message = "Data.user.";
            for (int i = 0; i < break_room.get(room_location).index.size(); i++) {
                idx = break_room.get(room_location).index.get(i);
                if (!clients.get(idx).isClose()) {
                    global_message += clients.get(idx).getID() + ";";
                }
            }
        }

        if (type == 1) {
            if (break_room.get(room_location).time == 0) {
                global_message = "Commands.Start";
                break_room.get(room_location).timer.cancel();
            } else {
                global_message = "Data.Timmer." + break_room.get(room_location).time;
                break_room.get(room_location).time--;
                SendStartMessage stt = new SendStartMessage(this, roomid);
                break_room.get(room_location).timer.schedule(stt, 1 * 1000);
            }
        }

        if (type == 2) {
            global_message = "Data.Score.";
            for (int i = 0; i < break_room.get(room_location).index.size(); i++) {
                idx = break_room.get(room_location).index.get(i);
                if (!clients.get(idx).isClose()) {
                    global_message += clients.get(idx).getID() + ":" + clients.get(idx).score + ";";
                }
            }
        }
        global_message += ".END";
        for (int i = 0; i < break_room.get(room_location).index.size(); i++) {
            idx = break_room.get(room_location).index.get(i);
            if (!clients.get(idx).isClose()) {
                clients.get(idx).sendMessage(global_message);

                System.out.print("\n\tSend to " + clients.get(idx).getID() + ":" + global_message + "\n");
            }
        }
        return true;
    }
}
